define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'packagerefund/index' + location.search,
                    table: 'watch_package_refund',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'user.username', title: __('User.username'), operate: 'LIKE'},
                        {field: 'order.deviceid', title:'设备号'},
                        {field: 'order_id', title: __('Order_id')},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'state',title: __('State'),searchList: {"0": __('State 0'),"1": __('State 1'),"2": __('State 2'),"3": __('State 3'),"4": __('State 4'),"5": __('State 5'),"6": __('State 6'),"7": __('State 7'),"8": __('State 8')},align: 'left',formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('查看退款'),
                                    text: '查看退款',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'packagerefund/detail'
                                },
                                {
                                    name: 'agreeGoods',
                                    title: __('同意退货'),
                                    text: '同意退货',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-check',
                                    confirm: '确认同意买家退款退货？',
                                    url: 'packagerefund/agree',
                                    visible: function(row) {
                                        if (row.state == 0) {
                                            return true;
                                        }
                                    },
                                    success: function(data, ret) {
                                        table.bootstrapTable('refresh');
                                        return false;
                                    },
                                    error: function(data, ret) {
                                        Toastr.error(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'receiving',
                                    title: __('确定收到买家退货'),
                                    text: '确认收货',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-check',
                                    confirm: '确定收到买家退货？确认后此退货订单自动完成退款！',
                                    url: 'packagerefund/receiving',
                                    visible: function(row) {
                                        if (row.state == 6) {
                                            return true;
                                        }
                                    },
                                    success: function(data, ret) {
                                        table.bootstrapTable('refresh');
                                        return false;
                                    },
                                    error: function(data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'refuse',
                                    title: __('拒绝退款'),
                                    text: '拒绝退款',
                                    classname: 'btn btn-xs btn-danger btn-dialog',
                                    icon: 'fa fa-times',
                                    url: 'packagerefund/refuse',
                                    visible: function(row) {
                                        if (row.state == 0) {
                                            return true;
                                        }
                                    },
                                    extend: 'data-area=["500px","270px"]'
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'packagerefund/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'packagerefund/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'packagerefund/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        refuse: function() {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
